$(function () {
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;

    var p = {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
    }

    initArtList();
    function initArtList() {
        $.ajax({
            method: 'GET',
            url: '/my/article/list',
            data: p,
            success: function (res) {
                // console.log(res);
                if (res.status !== 0) {
                    return layer.msg(res.message);
                }
                var htmlStr = template('tpl-table', res);
                $('tbody').html(htmlStr);
                renderPage(res.total);
            }
        });
    }

    template.defaults.imports.dataFormat = function (data) {
        var date = new Date(data);

        var y = date.getFullYear();
        var m = fillZero(date.getMonth() + 1);
        var d = fillZero(date.getDate());

        var hh = fillZero(date.getHours());
        var mm = fillZero(date.getMinutes());
        var ss = fillZero(date.getSeconds());

        return y + '-' + m + '-' + d + '  ' + hh + ':' + mm + ':' + ss;
    }

    function fillZero(n) {
        return n > 9 ? n : '0' + n;
    }

    initArtName();
    function initArtName() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: function (res) {
                // console.log(res);
                if (res.status !== 0) {
                    return layer.msg(res.message);
                }
                var htmlStr = template('tpl-cate', res);
                $('select[name=cate_id]').html(htmlStr);
                form.render();
            }
        });
    }

    $('#form-search').on('submit', function (e) {
        e.preventDefault();

        p.cate_id = $('select[name=cate_id]').val();
        p.state = $('select[name=state]').val();
        initArtList();
    });

    function renderPage(num) {
        laypage.render({
            elem: 'pageBox',
            count: num,
            limit: p.pagesize,
            curr: p.pagenum,
            limits: [1, 2, 3, 4, 5],
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            jump: function (obj, first) {
                // console.log(obj);
                // console.log(first);
                p.pagenum = obj.curr;
                p.pagesize = obj.limit;
                if (!first) {
                    initArtList();
                }
            }
        });
    }

    $('tbody').on('click', '.btn-delete', function () {
        var len = $('.btn-delete').length;
        // console.log(1);
        var id = $(this).data('id');
        // console.log(id);
        layer.confirm('确认删除?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                method: 'GET',
                url: '/my/article/delete/' + id,
                success: function (res) {
                    if (res.status !== 0) {
                        return layer.msg('删除文章失败！');
                    }
                    layer.msg('删除文章成功！');
                    if (len === 1) {
                        p.pagenum = (p.pagenum === 1 ? 1 : p.pagenum - 1);
                    }
                    initArtList();
                }
            });
            layer.close(index);
        });
    });

    var indexEdit = null;
    $('body').on('click', '.btn-edit', function () {
        indexEdit = layer.open({
            type: 1,
            title: '编辑类别',
            area: ['500px', '250px'],
            content: $('#dialog-eidt').html(),
        });

        var id = $(this).data('id');
        $.ajax({
            method: 'GET',
            url: '/my/article/cates/' + id,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message);
                }
                form.val('form-edit', res.data);
            }
        });
    });
    $('body').on('submit', '#form-edit', function (e) {
        e.preventDefault();

        $.ajax({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize(),
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message);
                }
                layer.close(indexEdit);
                initArtList();
            }
        });
    });
});